package com.wyd.qxglht.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.wyd.qxglht.dao.IUsersDao;
import com.wyd.qxglht.dto.UsersDTO;
import com.wyd.qxglht.model.Users;
import com.wyd.qxglht.service.IUsersService;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(path = "/users",method = RequestMethod.GET)
public class UsersController {

   
    
    @Resource
    private IUsersDao usersDao;
    @Resource
    private IUsersService usersService;

  

    @ApiOperation(value = "查询所有用户",notes = "查询所有用户")
    @GetMapping(value = "find")
    public Map<String ,Object> findAll(UsersDTO dto){
        Page<Users> page=usersService.findAllPage(dto);
       
        Map<String,Object> map=new HashMap<String ,Object>();

      
        map.put("code", 0);
        map.put("msg", "成功");
        map.put("count", page.getNumberOfElements());
        map.put("data", page.getContent());
        return map;
    }



    @ApiOperation(value = "添加用户",notes="添加用户")
      
   
    @PostMapping(value="add")
      
      public Users add(@RequestBody Users u) {
          
    
      
          return usersService.saveUsers(u);
          
      }


    @ApiOperation(value="根据编号修改用户信息",notes = "根据编号修改用户信息")
    @ApiImplicitParams({
      @ApiImplicitParam(name="id",value ="会员编号",required=true,paramType ="path"),
      @ApiImplicitParam(name="loginName",value ="登录名",required=true,paramType ="path"),
      @ApiImplicitParam(name="protectEMail",value ="邮箱",required=true,paramType ="path"),
      @ApiImplicitParam(name="ProtectMTel",value ="手机号",required=true,paramType ="path")
     
      
  })
  @PutMapping(value = "/edit/{id}/{loginName}/{protectEMail}/{protectMTel}")
  public int editUser(@PathVariable String id,@PathVariable String loginName,@PathVariable String protectEMail,@PathVariable String protectMTel) {
      
      return usersDao.editUser(id,loginName,protectEMail,protectMTel);
      
  }

  @ApiOperation(value="删除用户",notes = "删除用户")
  @DeleteMapping(value = "del")
  public int del( String id){

    return usersDao.delUser(id);

  }

  @ApiOperation(value="锁定用户",notes = "锁定用户")
  @PutMapping(value = "is")
  public int lockUser( String id){
System.out.println(id);
    return usersDao.lockoutUser(id);
  }

  @ApiOperation(value="解锁用户",notes = "解锁用户")
  @PutMapping(value = "/un")
  public int unLockUser( String id){

    return usersDao.notLockoutUser(id);
  }

  @ApiOperation(value = "登录",notes = "登录")//有bug
  @ApiImplicitParams({
    @ApiImplicitParam(name="loginName",value ="登录名",required=true,paramType ="path"),
    @ApiImplicitParam(name="password",value ="密码",required=true,paramType ="path")
   
    
})
  @GetMapping(value = "/login")
  public List<Users> login( String loginName, String password){


   List<Users> list=usersDao.login(loginName, password);

   if(list!=null){
     System.out.println(1);
    return list;
   }

   return null;

  }  
 
  @ApiOperation(value="重置密码",notes = "重置密码")
  @PutMapping(value = "reset")
  public int resetPwd( String id){

    return usersDao.pwdReset(id);
  }
  
  @ApiOperation(value="查询指定用户",notes = "查询指定用户")
  @GetMapping(value = "findOne")
  public Users findOne( String id){

    return usersDao.findOne(id);
  }

  


  

}